package com.my.screenadapter.searchpopwindow;

//import androidx.recyclerview.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import androidx.recyclerview.widget.RecyclerView;

import com.my.screenadapter.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张英杰 on 2015/12/09.
 * 适用于搜索
 */
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> {
    private final Object mLock = new Object();
    private String selectText;
    private final static int SKY_BLUE = 0xff008ee1;
    private List<FieldCache> mOriginalValues;
    private List<FieldCache> mObjects = new ArrayList<>();// 过滤后的item

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tv;

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }

    /**
     * ItemClick的接口回调
     *
     * @author 张英杰
     */
    public interface OnItemClickListener {
        void onItemClick(String content, int position, int id);
    }

    /**
     * ItemLongClick的接口回调
     *
     * @author 张英杰
     */
    public interface OnItemLongClickListener {
        void onItemLongClick(View v, int position, int id);
    }

    private OnItemClickListener mOnItemClickListener;

    public OnItemLongClickListener mOnItemLongClickListener;

    public void setOnClickListener(OnItemClickListener OnItemClickListener) {
        this.mOnItemClickListener = OnItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener OnItemLongClickListener) {
        mOnItemLongClickListener = OnItemLongClickListener;
    }

    public void setData(List<FieldCache> list, String selectText) {
        mOriginalValues = list;
        this.selectText = selectText;
        Log.i("list.size===", list.size() + "" + list.toString());
        notifyDataSetChanged();
        performFiltering(null);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.auto_seach_list_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(v);
        viewHolder.tv = (TextView) v.findViewById(R.id.tv_content);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
        viewHolder.tv.setDrawingCacheEnabled(false);
        final String parcelCode = mObjects.get(i).getParcelCode();
//        viewHolder.tv.setText(parcelCode);
        //设置内容
        if (selectText == null || selectText.equals("")) {
            viewHolder.tv.setText(parcelCode);
        } else {
            char[] reds = selectText.toCharArray();
            SpannableStringBuilder style = new SpannableStringBuilder(parcelCode);
            for (int j = 0; j < reds.length; j++) {
                int star = 0;
                for (; ; ) {
                    int fstart = parcelCode.indexOf(reds[j], star);
                    if (fstart != -1) {
                        style.setSpan(new ForegroundColorSpan(SKY_BLUE), fstart, fstart + 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                        star = fstart + 1;
                    } else {
                        break;
                    }
                }
            }
            viewHolder.tv.setText(style);
        }

        //如果设置了回调，则设置点击事件
        if (mOnItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(parcelCode, i, mObjects.get(i).getId());
                }
            });
        }
        //如果设置了长按点击回调，则设置点击事件
        if (mOnItemLongClickListener != null) {
            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemLongClickListener.onItemLongClick(viewHolder.itemView, i, mObjects.get(i).getId());
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mObjects != null ? mObjects.size() : 0;
    }

    /**
     * 匹配过滤搜索内容
     *
     * @param prefix 输入框中输入的内容
     */
    public void performFiltering(CharSequence prefix) {
        if (prefix == null || prefix.length() == 0) {//搜索框内容为空的时候显示所有历史记录
            selectText=null;
//            //只显示10条数据
//            if (mOriginalValues.size()>10){
//                ArrayList<FieldCache> searchAutoCaches = new ArrayList<>();
//                for (int i = 0; i < 10; i++) {
//                    searchAutoCaches.add(mOriginalValues.get(i));
//                }
//                mObjects = searchAutoCaches;
//            }else {
//                mObjects = mOriginalValues;
//            }
            //超10条数据
            mObjects = mOriginalValues;
        } else {
            selectText=prefix.toString().trim();
//            toUpperCase toLowerCase
//            备注：这两个方法只对英文字母有效，对除了A~Z和a~z的其余字符无任何效果。
            String prefixString = prefix.toString().toLowerCase();
            ArrayList<FieldCache> searchAutoCaches = new ArrayList<>();
            for (int i = 0; i < mOriginalValues.size(); i++) {
                String valueText = mOriginalValues.get(i).getParcelCode().toLowerCase();
                if (valueText.contains(prefixString) || valueText.startsWith(prefixString)) {
                    searchAutoCaches.add(mOriginalValues.get(i));
                }
            }
//            //只显示10条数据
//            if (searchAutoCaches.size()>10){
//                ArrayList<FieldCache> limits = new ArrayList<>();
//                for (int i = 0; i < 10; i++) {
//                    limits.add(searchAutoCaches.get(i));
//                }
//                mObjects = limits;
//            }else {
//                mObjects = searchAutoCaches;
//            }
            //超10条数据
            mObjects = searchAutoCaches;
//            mObjects = mOriginalValues;
        }
        notifyDataSetChanged();
    }
}
